package hu.szyrt.bug;

import java.util.ArrayList;

import hu.szyrt.bug.connections.FacebookConnection;
import hu.szyrt.bug.connections.LoginConnection;
import hu.szyrt.bug.dialogs.BUGDialog;
import hu.szyrt.bug.login.LoginHandler;
import hu.szyrt.bug.utils.Utils;
import hu.szyrt.bug.utils.Validator;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {
	
	private final static int REQUEST_REGISTRATION = 1;
	
	public final static int START_MAIN = 0,
							START_BACK = 1;
	
	private int startMode = 0;
	
	EditText email, password;
	Button skipButton, fbButton, registrationButton, loginButton;
	
	BUGDialog dialog;
	
	LoginHandler loginHandler;
	FacebookConnection facebookConnection;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {	
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		
		Bundle extras = getIntent().getExtras();
		if(extras != null && extras.containsKey("start_mode"))
			startMode = extras.getInt("start_mode");
		
		this.loginHandler = new LoginHandler(this);
		this.facebookConnection = new FacebookConnection(this);
		
		this.dialog = new BUGDialog(this);
		this.dialog.setTitle(R.string.errors);
		this.dialog.setPositiveButton(R.string.ok, null);
		
		email = (EditText)findViewById(R.id.email);
		password = (EditText)findViewById(R.id.password);
		
		skipButton = (Button)findViewById(R.id.skipButton);
		if(startMode == START_BACK){
			skipButton.setVisibility(View.GONE);
		}else{
			skipButton.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					start();
				}
			});
		}
		
		fbButton = (Button)findViewById(R.id.fbButton);
		fbButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				loginFB();
			}
		});		
		registrationButton = (Button)findViewById(R.id.registrationButton);
		registrationButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				registration();
			}
		});				
		loginButton = (Button)findViewById(R.id.loginButton);
		loginButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				login();
			}
		});		
	}
	
	// Logic --------------------------------------------------------
	private void login(){
		// Adatok levétele
		String email = this.email.getText().toString().trim();
		String password = this.password.getText().toString().trim();
		
		// Validálás
		ArrayList<String> errors = new ArrayList<String>();		
		if(!Validator.isValidEmail(email)) errors.add(getString(R.string.err_invalid_email_address));
		if(!Validator.isValidPassword(password)) errors.add(getString(R.string.err_invalid_password));
		
		if(errors != null && errors.size() > 0){
			dialog.setMessage(Utils.join(errors, "\n"));			
			dialog.show();
		}else{		
			
			// TODO: progress, amikor el kezdi a belépést
			
			// Belépés indítása
			if(Utils.isInternetAvailable(this)){
				final LoginConnection lc = new LoginConnection();
				lc.setEmail(email);
				lc.setPassword(password);
				lc.setNotify(new LoginConnection.Notify() {
					public void onSuccess() {
						loginHandler.setLoginUser(lc.getMemberID());
						if(loginHandler.isLoggedIn())
							start();
					}				
					public void onError() {
						dialog.setMessage(lc.getErrorText() == null ? lc.getResultText() : lc.getErrorText());				
						dialog.show();
					}
				});
				lc.start();	
			}else{
				Toast.makeText(this, R.string.err_no_internet, Toast.LENGTH_LONG).show();
			}
							
		}
	}
	
	private void loginFB(){
		// TODO: progress, amikor el kezdi a belépést
		if(Utils.isInternetAvailable(this)){
			facebookConnection.setNotify(new FacebookConnection.Notify() {
				public void onSuccess() {
					loginHandler.setLoginUser(facebookConnection.getMemberID());
					if(loginHandler.isLoggedIn())
						start();
				}
				public void onError() {
					dialog.setMessage(facebookConnection.getErrorText() == null ? facebookConnection.getResultText() : facebookConnection.getErrorText());				
					dialog.show();
				}
			});
			facebookConnection.start();
		}else{
			Toast.makeText(this, R.string.err_no_internet, Toast.LENGTH_LONG).show();
		}
	}
	
	private void start(){
		if(startMode == 0){
			startActivity(new Intent(LoginActivity.this, MainActivity.class));
			finish();
		}else{
			setResult(RESULT_OK);
			Toast.makeText(this, R.string.login_success, Toast.LENGTH_LONG).show();
			finish();
		}
	}
	
	private void registration(){
		startActivityForResult(new Intent(LoginActivity.this, RegistrationActivity.class), REQUEST_REGISTRATION);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode == REQUEST_REGISTRATION && resultCode == RESULT_OK)
			start();
		
		facebookConnection.facebookAuthorizeCallback(requestCode, resultCode, data);
	}
	
}
